#pragma once
#include "../Xml/Markup.h"

class CPatchSample
{
public:
	CString SamplePath;
	CArray<double> Feature;

	CPatchSample(CString& sPath, CArray<double>& feature)
		: SamplePath(sPath)
	{
		Feature.Copy(feature);
	}
	CPatchSample(CString& sPath, CString& sFeature)
		: SamplePath(sPath)
	{
		CString sF, sToken;
		sF = sFeature.Mid(1);
		sF.Remove(L'}');

		int iPos = 0;
		sToken = sF.Tokenize(L",", iPos).Trim();
		while(sToken.GetLength())
		{
			Feature.Add(_wtof(sToken));
			sToken = sF.Tokenize(L",", iPos).Trim();
		}
	}

	virtual ~CPatchSample()
	{
		Feature.RemoveAll();
	}

	CString GetFeatureString()
	{
		CString sRet(L"{ ");
		int iCnt = Feature.GetCount();
		for(int i = 0; i < iCnt; ++i)
		{
			sRet.AppendFormat(L"%.2lf, ", Feature[i]);
		}
		sRet.Append(L"}");
		return sRet;
	}

	void Serialize(CMarkup& xml)
	{
		xml.AddElem(L"PatchSample");
		xml.AddChildElem(L"Path", SamplePath);
		xml.AddChildElem(L"Feature", GetFeatureString());
	}

	static CPatchSample* Deserialize(CMarkup& xml)
	{
		CString sPath, sFeature;
		xml.IntoElem();
		if(xml.FindChildElem(L"Path"))	sPath = xml.GetChildData();
		if(xml.FindChildElem(L"Feature"))	sFeature = xml.GetChildData();
		xml.OutOfElem();

		if(sFeature.GetLength())
		{
			return new CPatchSample(sPath, sFeature);
		}
		return NULL;
	}

};

